/*
 * AddAccountFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


import com.insigma.yanwenlong.bookkeeping.pojo.Account;
import com.insigma.yanwenlong.bookkeeping.pojo.Member;
import com.insigma.yanwenlong.bookkeeping.service.AccountService;
import com.insigma.yanwenlong.bookkeeping.service.AccountServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.MemberService;
import com.insigma.yanwenlong.bookkeeping.service.MemberServiceImpl;
import com.insigma.yanwenlong.bookkeeping.util.GetNowTime;
import com.insigma.yanwenlong.bookkeeping.util.StringToDate;

/**
 *
 * @author  __USER__
 */
public class AddAccountFrame extends javax.swing.JFrame {

	private AccountService accountService = new AccountServiceImpl();
	private MemberService memberService = new MemberServiceImpl();
	/** Creates new form AddAccountFrame */
	public AddAccountFrame() {
		//设置标题
		setTitle("增加账户");
		//初始化界面
		initComponents();
		//设置此窗体关闭而不会关闭父窗体
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		//居中
		setLocationRelativeTo(null);
		//获取当前时间
		txtNowTime.setText(new GetNowTime().getNowTime());
		
		//初始化时显示所有人的成员
		showMember();
		
	}
	
	//得到所有人的name
	private void showMember() {
		try {
			List<Member> members = memberService.findallMembers();
			DefaultComboBoxModel dcb = new DefaultComboBoxModel();
			for (Member member : members) {
				dcb.addElement(member);
			}
			comboMember.setModel(dcb);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		txtAccountName = new javax.swing.JTextField();
		jLabel2 = new javax.swing.JLabel();
		spnMoney = new javax.swing.JSpinner();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jtaDesc = new javax.swing.JTextArea();
		btnAdd = new javax.swing.JButton();
		jLabel5 = new javax.swing.JLabel();
		txtNowTime = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		comboMember = new javax.swing.JComboBox();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		jLabel1.setText("\u8d26\u6237\u540d\u79f0");

		jLabel2.setText("\u521d\u59cb\u91d1\u989d");

		spnMoney.setModel(new javax.swing.SpinnerNumberModel(Double
				.valueOf(0.0d), Double.valueOf(0.0d), null, Double
				.valueOf(100.0d)));

		jLabel3.setText("\u521b\u5efa\u65e5\u671f");

		jLabel4.setText("\u8bf4\u660e");

		jtaDesc.setColumns(20);
		jtaDesc.setRows(5);
		jScrollPane1.setViewportView(jtaDesc);

		btnAdd.setText("\u521b\u5efa");
		btnAdd.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnAddActionPerformed(evt);
			}
		});

		jLabel5.setText("\u5143");

		jLabel6.setText("\u6240\u6709\u4eba");

		comboMember.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
				"Item 1", "Item 2", "Item 3", "Item 4" }));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				43,
																				43,
																				43)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																						.addComponent(
																								jLabel6,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel2,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel1,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								69,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel4,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel3,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addGap(
																												1,
																												1,
																												1)
																										.addComponent(
																												jScrollPane1,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												173,
																												javax.swing.GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								layout
																										.createSequentialGroup()
																										.addGroup(
																												layout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING,
																																false)
																														.addComponent(
																																comboMember,
																																javax.swing.GroupLayout.Alignment.LEADING,
																																0,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE)
																														.addComponent(
																																txtNowTime,
																																javax.swing.GroupLayout.Alignment.LEADING)
																														.addComponent(
																																spnMoney)
																														.addComponent(
																																txtAccountName,
																																javax.swing.GroupLayout.Alignment.LEADING,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																105,
																																Short.MAX_VALUE))
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												jLabel5))))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				99,
																				99,
																				99)
																		.addComponent(
																				btnAdd)))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addGap(36, 36, 36)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																txtAccountName,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(37, 37, 37)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel2)
														.addComponent(
																spnMoney,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel5))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												26, Short.MAX_VALUE)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel6)
														.addComponent(
																comboMember,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(27, 27, 27)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(
																txtNowTime,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(41, 41, 41)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel4)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																71,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(28, 28, 28)
										.addComponent(btnAdd)
										.addGap(26, 26, 26)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	//点击创建，将数据放到数据库中
	private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
		Member member = new Member();
		member = (Member)comboMember.getSelectedItem();
		try {
			Account account = new Account();
			account.setName(txtAccountName.getText());
			account.setInitAccount((Double) spnMoney.getValue());
			account.setCurrAccount((Double) spnMoney.getValue());
			account.setCreateTime(StringToDate.getDate(txtNowTime.getText()));
			account.setMemberId(member.getId());
			account.setNote(jtaDesc.getText());
			int rowAffected = accountService.addAccount(account);
			if (rowAffected > 0) {
				JOptionPane.showMessageDialog(this, "创建成功");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new AddAccountFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnAdd;
	private javax.swing.JComboBox comboMember;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTextArea jtaDesc;
	private javax.swing.JSpinner spnMoney;
	private javax.swing.JTextField txtAccountName;
	private javax.swing.JTextField txtNowTime;
	// End of variables declaration//GEN-END:variables

}